#include "number.h"
#include "converter.h"

void number::to_persian(const char *input, wchar_t *output, bool separator) {
  // persian digits        0       1       2       3       4       5       6       7       8       9
  const wchar_t math[] = { 0x06f0, 0x06f1, 0x06f2, 0x06f3, 0x06f4, 0x06f5, 0x06f6, 0x06f7, 0x06f8, 0x06f9 };

  wchar_t *out = output;
  string s;

  if (separator == true)
    converter::thousand_separator(input, s);
  else
    s = input;

  for (auto it = s.cbegin(); it != s.cend(); ++it) {
    if (*it == ',')
      *out = 0x066b; // persian thousand separator
    else if (*it == '.')
      *out = 0x066c; // persian comma
    else if (*it == '-')
      *out = 0x2212; // persian minux
    else if (isdigit((unsigned char) *it) != 0)
      *out = math[((int) (*it)) - 0x30]; // 0x30 is the ASCII code of 0
    else
      *out = (wchar_t) *it;

    ++out;
  } // for i

  *out = L'\0';
} // to_persian

void number::to_arabic(const char *input, wchar_t *output, bool separator) {
  // arabic digits         0       1       2       3       4       5       6       7       8       9
  const wchar_t math[] = { 0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, 0x0668, 0x0669 };

  wchar_t *out = output;
  string s;

  if (separator == true)
    converter::thousand_separator(input, s);
  else
    s = input;

  for (auto it = s.cbegin(); it != s.cend(); ++it) {
    if (*it == ',')
      *out = 0x066b; // arabic thousand separator
    else if (*it == '.')
      *out = 0x066c; // arabic comma
    else if (*it == '-')
      *out = 0x2212; // arabic minux
    else if (isdigit((unsigned char) *it) != 0)
      *out = math[((int) (*it)) - 0x30]; // 0x30 is the ASCII code of 0
    else
      *out = (wchar_t) *it;

    ++out;
  } // for i

  *out = L'\0';
} // to_arabic

void number::to_german(const char *input, wchar_t *output, bool separator) {
  wchar_t *out = output;
  string s;

  if (separator == true)
    converter::thousand_separator(input, s);
  else
    s = input;

  for (auto it = s.cbegin(); it != s.cend(); ++it) {
    if (*it == ',')
      *out = L'.'; // german thousand separator
    else if (*it == '.')
      *out = L','; // german comma
    else
      *out = (wchar_t) (*it);

    ++out;
  } // for i

  *out = L'\0';
} // to_german
